﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.sim {
    import Engine.Classes.*;
    import Classes.*;
    import Classes.actions.*;
    import Engine.*;

    public class PeepActionSelection implements IActionSelection {

        public static const GAME_EVENT:String = "NPCEnterAction";

        protected var m_peep:Peep;
        protected var m_totalMerchantPopularity:int;

        public function PeepActionSelection(_arg1:Peep){
            this.m_peep = _arg1;
            this.m_totalMerchantPopularity = 0;
        }
        public function getNextActions():Array{
            var _local1:Number = this.m_peep.lastMerchantId;
            var _local2:SelectionResult = SelectionResult.FAIL;
            this.m_peep.beforeDecision();
            if (this.m_peep.isReadyToGoHome()){
                _local2 = this.goHomeAndLeave();
            };
            if ((((_local2 == SelectionResult.FAIL)) && (!((_local1 == 0))))){
                _local2 = this.wanderOrGoHome();
            };
            if (_local2 == SelectionResult.FAIL){
                _local2 = this.goToMerchant();
            };
            if (_local2 == SelectionResult.FAIL){
                _local2 = this.goToPOIOrHouse();
            };
            if (_local2 == SelectionResult.FAIL){
                _local2 = new SelectionResult(null, [new ActionDie(this.m_peep)]);
            };
            return (_local2.actions);
        }
        protected function poiMerchantFilter(_arg1:POI):Boolean{
            var _local2:Boolean;
            if (!(_arg1.resource is IMerchant)){
                _local2 = true;
            };
            return (_local2);
        }
        protected function goToPOIOrHouse():SelectionResult{
            var _local8:Boolean;
            var _local1:Array = Global.world.citySim.poiManager.getResourcesByPredicate(this.poiMerchantFilter);
            var _local2:Array = Global.world.getObjectsByClass(Residence);
            if (_local2 == null){
                _local2 = [];
            };
            var _local3:int = (_local2.length + _local1.length);
            var _local4:Number = ((_local3)==0) ? 0 : (_local2.length / _local3);
            var _local5:Number = Math.random();
            if (_local5 < _local4){
                _local8 = (Math.random() < Global.gameSettings.getNumber("npcGoingHomeFraction", 0.5));
                return ((_local8) ? this.goHomeAndLeave() : this.goToHouse());
            };
            if (_local1.length == 0){
                return (SelectionResult.FAIL);
            };
            var _local6:MapResource = MathUtil.randomElement(_local1);
            var _local7:int = Global.gameSettings.getInt("npcBizWaitingTime", 50);
            return (new SelectionResult(_local6, [new ActionEnableFreedom(this.m_peep, false), new ActionNavigate(this.m_peep, _local6, null).setPathType(RoadManager.PATH_FULL), new ActionPlayAnimation(this.m_peep, "jump", 0.7), new ActionBusinessPacing(this.m_peep, _local6, false, _local7), new ActionEnableFreedom(this.m_peep, true)]));
        }
        protected function wanderOrGoHome():SelectionResult{
            var _local1:Number = Global.gameSettings.getNumber("npcWanderProb", 0.8);
            var _local2:Number = Math.random();
            if (_local2 > _local1){
                return (this.goHomeAndLeave());
            };
            var _local3:Number = Global.gameSettings.getNumber("npcWanderTimeSec", 15);
            var _local4:Number = Global.gameSettings.getNumber("npcWanderTimePlusMinus", 5);
            var _local5:Number = MathUtil.random((_local3 + _local4), (_local3 - _local4));
            return (new SelectionResult(null, [new ActionEnableFreedom(this.m_peep, false), new ActionNavigateRandom(this.m_peep).setTimeout(_local5), new ActionEnableFreedom(this.m_peep, true)]));
        }
        protected function merchantChecker(_arg1:WorldObject):Boolean{
            var _local2:Boolean;
            if ((((_arg1 is IMerchant)) && ((_arg1 as IMerchant).isRouteable()))){
                if ((_arg1 as IMerchant).getId() !== this.m_peep.lastMerchantId){
                    this.m_totalMerchantPopularity = (this.m_totalMerchantPopularity + (_arg1 as IMerchant).getPopularity());
                    _local2 = true;
                };
            };
            return (_local2);
        }
        protected function goToMerchant():SelectionResult{
            var target:* = null;
            var merchant:* = null;
            var time:* = 0;
            this.m_totalMerchantPopularity = 0;
            var choices:* = Global.world.getObjectsByPredicate(this.merchantChecker);
            if (choices.length == 0){
                return (SelectionResult.FAIL);
            };
            var valve:* = (Math.random() * this.m_totalMerchantPopularity);
            target = null;
            for each (merchant in choices) {
                valve = (valve - merchant.getPopularity());
                if (valve < 0){
                    target = merchant.getMapResource();
                    break;
                };
            };
            this.m_totalMerchantPopularity = 0;
            time = Global.gameSettings.getInt("npcBizWaitingTime", 50);
            return (new SelectionResult(target, [new ActionFn(this.m_peep, function ():void{
                m_peep.lastMerchantId = target.getId();
            }), new ActionEnableFreedom(this.m_peep, false), new ActionNavigate(this.m_peep, target, null).setPathType(RoadManager.PATH_TO_FRONT_ENTRANCE).setTeleportOnFailure(true), new ActionEnableFreedom(this.m_peep, true), new ActionBusinessEnter(this.m_peep, (target as IMerchant))]));
        }
        protected function goToHouse():SelectionResult{
            var _local1:Array = Global.world.getObjectsByClass(Residence);
            if (_local1.length == 0){
                return (SelectionResult.FAIL);
            };
            var _local2:MapResource = MathUtil.randomElement(_local1);
            var _local3:int = Global.gameSettings.getInt("npcPoiWaitingTime", 50);
            return (new SelectionResult(_local2, [new ActionEnableFreedom(this.m_peep, false), new ActionNavigate(this.m_peep, _local2, null).setPathType(RoadManager.PATH_TO_FRONT_ENTRANCE).setTeleportOnFailure(true), new ActionEnableFreedom(this.m_peep, true), new ActionBusinessPacing(this.m_peep, _local2, false, _local3)]));
        }
        protected function goHomeAndLeave():SelectionResult{
            var home:* = null;
            var actions:* = [];
            home = this.m_peep.getHome();
            if (home == null){
                home = Global.world.citySim.npcManager.getRandomReturnHome();
            };
            if (home != null){
                actions = [new ActionEnableFreedom(this.m_peep, false), new ActionNavigate(this.m_peep, home, null).setPathType(RoadManager.PATH_FULL).setTeleportOnFailure(true), new ActionFn(this.m_peep, function ():void{
                    Global.world.citySim.npcManager.onResidentReturnHome(home);
                })];
            };
            actions.push(new ActionDie(this.m_peep));
            return (new SelectionResult(home, actions));
        }

    }
}//package Classes.sim 
